#ifndef DETECTCACHEMODEL_H
#define DETECTCACHEMODEL_H
#include "imagerecord.h"

#include <QString>
#include <QAbstractListModel>
#include <QVector>
#include <QMutex>
#include "AutoPropertyHelper.h"
class DetectCacheModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(int currentCacheNum  READ currentCacheNum NOTIFY currentCacheNumChange)
    Q_PROPERTY(int currentCacheSeclectId READ currentCacheSeclectId WRITE setcurrentCacheSeclectId NOTIFY currentCacheSeclectIdChange)
    Q_PROPERTY(int okNum READ okNum  NOTIFY okNumChange) //

public:
    enum DataRoles {
        IdRole = Qt::UserRole + 1,
        DetectDetailRole,
        DetectTimeRole,
        DetectIsOKRole
    };

    struct DataItem {
        int id;
        int time;
        bool isOK;
        QString detail;
        QVariantMap detectStepInfo;
        QVariantMap detectStepTime;
        // cv::Mat baseImage;
        cv::Mat preProgeressImage;
        cv::Mat mainImage;
        NgCard ngCard;
    };

    // struct CardCacheItem {
    //     int id;
    //     ImageRecord card;
    // };
    AUTO_CONFIG_PROPERTY(DetectCacheModel,"base",int ,cachequantity)

    Q_INVOKABLE void setCacheNum(int cacheNum);
    Q_INVOKABLE void ClearCacheNum();
    Q_INVOKABLE void DisplayCurrentSummary();
    Q_INVOKABLE void DisplayTotalSummary();
    Q_INVOKABLE void DisplayFileSummary(QString filePath);
    Q_INVOKABLE void outCSVFileSummary(QString filePath);
    Q_INVOKABLE void clearOkNum();


    void addOkNum();
    int okNum() const {return okNum_;};
    void displayImageRecordById(int id);
    ImageRecord getidImageCard(int id);
    int  currentCacheSeclectId() const {return currentCacheSeclectId_;};
    void setcurrentCacheSeclectId(int id);
    static DetectCacheModel& instance();


    int currentCacheNum() const { return currentCacheNum_; };
    void displaycurrentCacheNum(int currentCacheNum);
    void clearIndex();
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QHash<int, QByteArray> roleNames() const override;


    void updateDefectById(int id, std::vector<int> defect);
    void changeItemByID(int id, QString detail);
    DataItem &getItemByID(int id);
    void inPlaceadjustCache(int currentIndex, int offset);
    void inPlaceadjustCacheRemove34(int currentId, int offset);
public slots:
    void valueTableLoad();
    void addDataItem(ImageRecord imgCard);

signals:
    void finishAddDetectCache(int id , ImageRecord imgCard);
    void clearDetectCache();
    void currentCacheNumChange();
    void currentCacheSeclectIdChange();
    void drawistanceLargeSmall(int id ,float top,float bottom,
                               int topmax,int topmin,int bottommax,int bottommin);
    void okNumChange();

private:
    QVector<DataItem> m_cache;
    //QVector<CardCacheItem> m_imgCardCache;
    DetectCacheModel(QObject *parent = nullptr);
    DetectCacheModel(const DetectCacheModel&) = delete;
    DetectCacheModel& operator=(const DetectCacheModel&) = delete;
    int currentCacheNum_;
    int cacheNum_ ;
    int index;
    int currentCacheSeclectId_;
    int okNum_;
    mutable QMutex mutex_; // 用于保护多线程访问
};

#endif // DETECTCACHEMODEL_H
